/*
 * CS_SOCIAL_REQUEST_CONNECT_SM_V2.cpp
 *
 *  Created on: 2014. 2. 13.
 *      Author: zenoniaonline
 */



#include "../../Command.h"
#include "../../User.h"
#include "../../Proxy.h"
#include "../../Enum.h"
#include "../../../../protocol/iFriends.h"
#include "../../NResource/Static.h"

#include "../../NLogic/Function.h"

#include "../../NProcess/Static.h"

#include <sstream>

namespace ProjectZ { namespace NCommand {

void SendPushCONNECT_SM_V2(User * pUser, int receiveUserSeq)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT company, sale_cd, uuid FROM info_user WHERE user_seq = %d", receiveUserSeq);
	if (false == ::Database::GETSESSION()->SyncExecute(static_cast<int>(DatabaseType::GAME_SLAVE), clsAuto))
		return;

	if( clsAuto->GetErrorIdx() != 0 )
	{
		DEBUGLOG("%s clsAuto->GetErrorIdx() != 0", __FUNCTION__ );
		return;
	}

	if (0 == clsAuto->ResultRowCount())
	{
		DEBUGLOG("%s 0 == clsAuto->ResultRowCount()", __FUNCTION__ );
		return;
	}

	int company = 0;
	int sale_cd = 0;
	::Toolkit::Str<81> uuid;

	while( clsAuto->Next() )
	{
		clsAuto->GetRecord() >> company >> sale_cd >> uuid;
	}


	{
		//NProcess::PushSender::Param clsParam;

		NProcess::PushSender::Param * clsParam = NProcess::Static::Instance().GetParamPool()->SAFE_NEW();
		if (company == 5)
			clsParam->_eType = NProcess::PushSender::Param::Type::ANDROID;
		else
			clsParam->_eType = NProcess::PushSender::Param::Type::IOS;

//		clsParam->_sender = "주인님~ 제노하러 가요!";
//
//		std::stringstream stream;
//		stream << "[";
//		stream << pUser->GetUserInfo().nickname.c_str();
//		stream << "]님이 기다리고 있어요!!! 빨리 제노 온라인 젒고 고고싱이요~~";
//
//		clsParam->_message = stream.str();
//		clsParam->_uuid = uuid.c_str();

		NResource::MessageResource * pMessageResource = NResource::Static::InstancePtr()->GetMessageResource();
		if (NULL == pMessageResource)
			return;

		std::string sender;
		std::string message;

		if (false == pMessageResource->SetMessageFromType(company, sale_cd, NResource::MessageData::message_type::request_connect, sender, message))
			return;

		Toolkit::Str<400> sendMessage;
		sendMessage.Format(message.c_str(), pUser->GetUserInfo().nickname.c_str());

		clsParam->_sender	= sender.c_str();
		clsParam->_message	= sendMessage.c_str();
		clsParam->_uuid		= uuid.c_str();

		NProcess::Static::Instance().GetPushSender().Push(clsParam);
	}
		//NLogic::NFunction::SendPush(msg.gid, stream.str());
}

void Command::CS_SOCIAL_REQUEST_CONNECT_SM_V2(User * pUser, ::Network::Packet * pPacket)
{
	/*
	CS_SOCIAL_REQUEST_CONNECT_SM_V2              0x215C
	{
		u32 u32gid;                      // 상대방 GID
		u8 u8push_msg_type;         // 푸쉬 메시지 유형 (0 : 카톡 메시지(클라이언트), 1: 서버 푸시)
	}
	 */
	PREPARE_PACKET;
	RequestConnectSyn msg;

	uint8_t u8push_msg_type = 0;

	*pPacket >> msg.gid >> u8push_msg_type;

	if( msg.gid <= 0 )
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->MsgCat("%2d", ACK_UNKNOWN_ERROR);

		pAck->MakePacket(CMD_SC_SOCIAL_REQUEST_CONNECT_SM);
		pUser->Send(pAck);
		return;
	}

	NResource::BalanceResource * balanceResource = NResource::Static::InstancePtr()->GetBalance();
	msg.heart_count_max = balanceResource->GetValue(pUser, EM_FRIEND_POINT_MAX);
	msg.sleeping_user_checking_day_const = balanceResource->GetValue(pUser, EM_SLEEPING_USER_CHECKING_DAY_CONST);

	NResource::SocialRewardResource * socialReward = NResource::Static::InstancePtr()->GetSocialReward();
	NResource::SocialRewardData::Flyweight * rewardData = socialReward->GetFly(pUser, NResource::SocialRewardData::NoPlayFriend);
	msg.sleeping_user_invite_heart_point_reward = rewardData->_SEND_REWARD * rewardData->_IS_EVENT;
	msg.sleeping_user_invite_heart_point_reward_max = balanceResource->GetValue(pUser, EM_SLEEPING_USER_INVITE_HEART_POINT_REWARD_MAX);
	msg.sleeping_user_be_invited_heart_point_reward = rewardData->_RECV_REWARD * rewardData->_IS_EVENT;
	msg.sleeping_user_be_invited_heart_point_reward_max = balanceResource->GetValue(pUser, EM_SLEEPING_USER_BE_INVITED_HEART_POINT_REWARD_MAX);
	msg.msg_type = rewardData->_MSG_TYPE;

	::ProjectZ::NProxy::SendFriendsServer(pUser->GetUserSeq(), msg);

//	std::stringstream stream;
//	stream << "주인님!! [";
//	stream << pUser->GetUserInfo().nickname.c_str();
//	stream << "]님이 기다린다고 빨리 오시래요!! 올 때까지 기다린다고..";
//
//	NLogic::NFunction::SendPush(msg.gid, stream.str());

	if (1 == u8push_msg_type)
		SendPushCONNECT_SM_V2(pUser, msg.gid);
}

} /*NCommand*/ } /*ProjectZ*/


